<?php

	/**
	 * File : html_header.php
	 * Description : Fonctions et définitions de l'entête HTML
	 * Date : 07.08.2012 15:02:04
	 * 
	 * This file is part of Phenix.  Phenix is free software: you can
	 * redistribute it and/or modify it under the terms of the GNU General Public
	 * License as published by the Free Software Foundation, version 2.
	 *
	 * This program is distributed in the hope that it will be useful, but WITHOUT
	 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
	 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
	 * details.
	 *
	 * You should have received a copy of the GNU General Public License along with
	 * this program; if not, write to the Free Software Foundation, Inc., 51
	 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
	 *
	 * Nils Schaetti <n.schaetti@gmail.com>
	**/

	// Cette classe représente un entête HTML
	class ktkHtmlHeader
	{
	
		/********************
		 * Propriétés
		 ********************/
		 
		// Fichiers CSS
		private $css_files;
		
		// Fichiers JavaScript
		private $js_files;
		
		// Fichier CSS de KTK
		private $ktk_css_file;
		
		// Fichier JS de KTK
		private $tkt_js_file;
		
		// Titre de la page
		private $Title;
		
		// Au chargement
		private $onLoad;
		
		/********************
		 * Constructeur
		 ********************/
		function ktkHtmlHeader($ktk_css_file, $ktk_js_file, $Title = '')
		{
			// CSS
			$this->ktk_css_file = $ktk_css_file;
			$this->ktk_js_file = $ktk_js_file;
			
			// Init
			$this->css_files = array();
			$this->js_files = array();
			
			// Titre
			$this->Title = $Title;
		}
		
		/********************
		 * Fonctions
		 ********************/
		 
		/*
		 * Ajoute un fichier CSS
		 */
		function addCSSFile($css_file)
		{
			$this->css_files[] = $css_file;
		}
		
		/*
		 * Ajoute un fichier JavaScript
		 */
		function addJSFile($js_file)
		{
			$this->js_files[] = $js_file;
		}
		
		/**
		 * Ajoute un appel au chargement
		 */
		function setOnLoad($onLoad)
		{
			$this->onLoad = $onLoad;
		}
		
		/*
		 * Renvoi le code HTML
		 */
		function getCode()
		{
			// Fichiers CSS
			$css_code = '';
			
			// Fichier Javascript
			$js_code = '';
		
			// Propriétés
			$ktk_css_file = $this->ktk_css_file;
			$ktk_js_file = $this->ktk_js_file;
		
			// Code
			$code = <<< __DELIMITER__
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>$this->Title</title>
        <link rel="stylesheet" href="$ktk_css_file" type="text/css">
        @@_CSS_@@
        @@_JS_@@
</head>
<body id="ktkBody" onLoad="@@_ONLOAD_@@">
__DELIMITER__;

			// Pour chaque fichier CSS
			$css_files = $this->css_files;
			foreach($css_files as $css_file)
				$css_code .= '<link rel="stylesheet" href="'.$css_file.'" type="text/css">';
				
			if(is_dir($ktk_js_file))
			{
				// Ouvre le répertoire des fichiers javascript
				$js_dir = opendir($ktk_js_file);
			
				// Pour chaque fichier du répertoire
				while(($file = readdir($js_dir)) !== false)
				{
					// Pas de . et de ..
					if($file !== '..' && $file !== '.')
						array_unshift($this->js_files,$ktk_js_file.'/'.$file);
				}
				
				// Ferme le répertoire
				closedir($js_dir);
			}
				
			// Pour chaque fichier JS
			$js_files = $this->js_files;
			foreach($js_files as $js_file)
				$js_code .= '<script type="text/javascript" src="'.$js_file.'"></script>';

			// CSS
			$code = str_replace('@@_CSS_@@', $css_code, $code);
			
			// JS
			$code = str_replace('@@_JS_@@', $js_code, $code);
			
			// onLoad
			$code = str_replace('@@_ONLOAD_@@', $this->onLoad, $code);
			
			return $code;
		}
		
		/*
		 * Affiche le code
		 */
		function Show()
		{
			echo $this->getCode();
		}
	
	}

?>
